﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AddressBookModel
{
    public class ContactFactory
    {

        private DateTime date;
        private bool isDate;

        public ContactFactory()
        {
        }

        public void CreateContacts(string serializedContacts)
        {
            var contacts = serializedContacts.Split(new string[] { Environment.NewLine },
                            StringSplitOptions.RemoveEmptyEntries);

            Contact contact;
            foreach (var person in contacts)
            {
                if (person.StartsWith(ContactGroup.Family.ToString()))
                {
                    contact = new FamilyContact();
                    Contact.AddContact(contact.Deserialize(person));
                }
                else if (person.StartsWith(ContactGroup.Business.ToString()))
                {
                    contact = new BusinessContact();
                    Contact.AddContact(contact.Deserialize(person));
                }
                else if (person.StartsWith(ContactGroup.Friends.ToString()))
                {
                    contact = new FriendContact();
                    Contact.AddContact(contact.Deserialize(person));
                }
                else if (person.StartsWith(ContactGroup.Other.ToString()))
                {
                    contact = new OtherContact();
                    Contact.AddContact(contact.Deserialize(person));
                }
                else continue;
            }
        }

        public Contact CreateFamilyContact(string name, string telNumber, string email, string birthday)
        {
            this.isDate = DateTime.TryParse(birthday.Trim(), out this.date);
            return new FamilyContact(name, telNumber, email, this.isDate ? this.date : (DateTime?)null);
        }

        public Contact CreateFriendContact(string name, string telNumber, string email, string birthday)
        {
            this.isDate = DateTime.TryParse(birthday.Trim(), out this.date);
            return new FriendContact(name, telNumber, email, this.isDate ? this.date : (DateTime?)null);
        }

        public Contact CreateBusinessContact(string name, string telNumber, string email, string birthday)
        {
            this.isDate = DateTime.TryParse(birthday.Trim(), out this.date);
            return new BusinessContact(name, telNumber, email, this.isDate ? this.date : (DateTime?)null);
        }

        public Contact CreateOtherContact(string name, string telNumber, string email, string birthday)
        {
            this.isDate = DateTime.TryParse(birthday.Trim(), out this.date);
            return new OtherContact(name, telNumber, email, this.isDate ? this.date : (DateTime?)null);
        }
    }
}
